package com.richapp.launcher.database.infrared.dao;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.richapp.launcher.database.infrared.InfraredDatabaseHelper;
import com.richapp.launcher.database.infrared.mode.Brand;
import com.richapp.launcher.database.infrared.mode.InfraredVersion;

import java.sql.SQLException;

/**
 * Created by yif on 16/9/6.
 */
public class InfraredVersionDao {


    private Context context;
    private InfraredDatabaseHelper helper = null;
    private Dao<InfraredVersion, Integer> dao = null;

    public InfraredVersionDao(Context context) {
        this.context = context;
        try {
            helper = InfraredDatabaseHelper.getHelper(this.context);
            dao = helper.getDao(InfraredVersion.class);
        } catch (Exception e) {
            Log.e("error", e.getMessage());
        }
    }

    public InfraredVersion getFirst() {
        InfraredVersion infraredVersion = null;
        try {
            QueryBuilder queryBuilder = dao.queryBuilder();
            Object obj = queryBuilder.queryForFirst();
            if (obj instanceof InfraredVersion) {
                infraredVersion = (InfraredVersion) obj;
            }
        } catch (Exception e) {
            Log.e("error", e.getMessage());
        }
        return infraredVersion;
    }

    public void deleteAll() {
        DeleteBuilder deleteBuilder = dao.deleteBuilder();
        try {
            deleteBuilder.delete();
        } catch (SQLException e) {
            Log.e(InfraredVersionDao.class.getName(), e.getMessage(), e);
        }
    }

    public void add(InfraredVersion infraredVersion) {
        try {
            int count = dao.create(infraredVersion);
            Log.e(InfraredVersionDao.class.getName(), count + "");
        } catch (Exception e) {
            Log.e(InfraredVersionDao.class.getName(), e.getMessage());
        }
    }

    public void update(InfraredVersion infraredVersion) {
        try {
            dao.update(infraredVersion);
        } catch (Exception e) {
            Log.e(InfraredVersionDao.class.getName(), e.getMessage());
        }
    }
}
